package com.ming.controller.admin;

import com.ming.dto.SetmealDTO;
import com.ming.dto.SetmealPageQueryDTO;
import com.ming.entity.Setmeal;
import com.ming.result.PageResult;
import com.ming.result.Result;
import com.ming.service.SetmealService;
import com.ming.vo.SetmealVOWithDishes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐：{}", setmealDTO);
        setmealService.saveWithDishes(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询：{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealVOWithDishes> getById(@PathVariable Long id) {
        log.info("根据ID查询套餐：{}", id);
        SetmealVOWithDishes setmealVOWithDishes = setmealService.getByIdWithDishes(id);
        return Result.success(setmealVOWithDishes);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐：{}", setmealDTO);
        setmealService.updateWithDishes(setmealDTO);
        return Result.success();
    }

    /**
     * 启用或禁用套餐
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("启用或禁用套餐：{}, {}", status, id);
        setmealService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 根据ID删除套餐
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        log.info("根据ID删除套餐：{}", id);
        setmealService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据ID批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("根据ID批量删除套餐：{}", ids);
        setmealService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据分类ID查询套餐
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<Setmeal>> list(Long categoryId) {
        log.info("根据分类ID查询套餐：{}", categoryId);
        List<Setmeal> setmeals = setmealService.getByCategoryId(categoryId);
        return Result.success(setmeals);
    }
}